import { ApiHideProperty, ApiProperty } from '@nestjs/swagger';
import { Exclude, Transform } from 'class-transformer';
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
//实体会映射为数据库的一张表
@Entity()
export class User {
  @PrimaryGeneratedColumn()
  @ApiProperty({ description: '用户ID', example: 1 })
  id: number;

  @Column({ length: 50, unique: true })
  @ApiProperty({ description: '用户名', example: 'nick' })
  username: string;

  @Column()
  @Exclude()
  @ApiHideProperty() //表示这是一个隐藏字段不会在swageer中出现
  password: string;

  @Column({ length: 15, nullable: true })
  @Transform(
    ({ value }) => value && value.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$3'),
  )
  @ApiProperty({ description: '手机号', example: '12345678901' })
  mobile: string;

  @Column({ length: 100, nullable: true })
  @ApiProperty({ description: '邮件', example: 'nick@qq.com' })
  email: string;

  @Column({ default: 1 })
  @ApiProperty({ description: '状态', example: 1 })
  status: number; //是否生效 0表示无效 1表示生效

  @Column({ default: false })
  @ApiProperty({ description: '是否是超级管理员', example: true })
  is_super: boolean; //是否是超级管理

  @Column({ default: 100 })
  @ApiProperty({ description: '排序', example: 1 })
  sort: number; //排序编号

  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  @ApiProperty({ description: '创建时间', example: '2024-01-01' })
  createAt: Date;

  @Column({
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
    onUpdate: 'CURRENT_TIMESTAMP',
  })
  @ApiProperty({ description: '更新时间', example: '2024-01-01' })
  updateAt: Date;
}
